<template>
  <div class="custom-sidebar" :class="{ collapsed: isCollapsed }">
    <!-- 菜单折叠按钮 -->
    <div class="collapse-btn" @click="toggleCollapse">
      <el-icon v-if="isCollapsed">
        <Expand />
      </el-icon>
      <el-icon v-else>
        <Fold />
      </el-icon>
    </div>
    <!-- 菜单项 -->
    <div v-for="(item, i) in menuItems" :key="i" class="menu-item" :class="{ active: activeItem === item.meta.name }"
      @click="selectItem(item)">
      <el-tooltip effect="dark" :content="item.meta.title" placement="right" :disabled="!isCollapsed">
        <div class="menu-content">
          <el-icon class="menu-icon">
            <component :is="item.meta.icon" />
          </el-icon>
          <span class="menu-title" v-show="!isCollapsed">{{ item.meta.title }}</span>
        </div>
      </el-tooltip>
    </div>

    <!-- 底部用户区域 -->
    <div v-if="userType === 0" class="user-area !pb-2" v-show="!isCollapsed">
      <h3 class="whitespace-nowrap">{{ currEnterprise?.enterpriseName }}</h3>
      <div class="mt-5 whitespace-nowrap text-right">
        <el-tag :type="currEnterprise?.authenticationFlag === '1' ? 'primary' : 'danger'">{{
          currEnterprise?.authenticationFlag
            ===
            '1' ? '已认证' : '未认证' }}</el-tag>
        <el-tag :type="currEnterprise?.realNameFlag === '1' ? 'primary' : 'danger'" class="ml-3">{{
          currEnterprise?.realNameFlag
            === '1' ? '已实名' : '未实名' }}</el-tag>
      </div>

      <el-dropdown trigger="click" @command="toggleEnterpriseFun" placement="bottom">
        <el-button type="primary" class="mt-5">切换企业</el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item :command="item" v-for="(item, i) in enterpriseList" :key="i">{{ item.enterpriseName
              }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getEenterpriceList, swithMainSign } from '@/api/enterprise';
import useUserStore from '@/stores/user';
import {
  Fold,
  Expand,
} from '@element-plus/icons-vue';
const router = useRouter()
const route = useRoute()
const storeUser = useUserStore()
const { enterprise, currEnterprise, userType } = storeToRefs(storeUser)

// console.log(userStore);

// 响应式状态
const isCollapsed = ref(false);
const activeItem = ref(route.meta.name);
const props = defineProps(["menuItems"])


// 用户信息


// 选择菜单项
const selectItem = (item) => {
  activeItem.value = item.meta.name;
  console.log(item);

  router.push({
    name: item.name
  })
};

// 切换企业
function toggleEnterpriseFun(params) {
  swithMainSign({ creditCode: params.creditCode }).then(res => {
    if (res.code === 200) {
      ElMessage.success('切换成功')
    }
  }).then(() => {
    storeUser.setCurrEnterprise(params)
  })
}

// 切换折叠状态
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

const enterpriseList = ref([]);
// 初始化时根据窗口宽度自动折叠
onMounted(() => {
  if (window.innerWidth < 992) {
    isCollapsed.value = true;
  }

  getEenterpriceList().then(res => {
    if (res.code === 200) {
      enterpriseList.value = res.data;
    }
  });
});
</script>

<style scoped lang="scss">
:deep(.el-form-item) {
  margin-right: 12px !important;
}

.custom-sidebar {
  height: 100%;
  width: 260px;
  background: linear-gradient(135deg, #555E77, #424d6a);
  color: #fff;
  padding: 20px 0;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 1000;
  display: flex;
  flex-direction: column;
}

.custom-sidebar.collapsed {
  width: 64px;
}

.collapse-btn {
  display: flex;
  justify-content: flex-end;
  padding: 0 20px 20px;
  margin-bottom: 15px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  transition: all 0.3s;
}

.collapse-btn .el-icon {
  color: #fff;
  font-size: 24px;
  transition: all 0.3s;
}

.collapse-btn:hover .el-icon {
  color: #409eff;
  transform: scale(1.1);
}

.menu-item {
  padding: 0 20px;
  margin-bottom: 5px;
  cursor: pointer;
  transition: all 0.3s;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

.menu-item.active {
  background: rgba(64, 158, 255, 0.2);
  border-left: 4px solid #409eff;
}

.menu-content {
  display: flex;
  align-items: center;
  padding: 14px 0;
}

.menu-icon {
  font-size: 20px;
  margin-right: 15px;
  min-width: 24px;
  display: flex;
  justify-content: center;
}

.menu-title {
  font-size: 15px;
  font-weight: 500;
  transition: opacity 0.3s;
  white-space: nowrap;
}

.collapsed .menu-title {
  opacity: 0;
  width: 0;
}

.user-area {
  padding: 20px;
  margin-top: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.user-info {
  margin-left: 12px;
}

.user-name {
  font-size: 15px;
  font-weight: 500;
}

.user-role {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  margin-top: 4px;
}

/* 响应式设计 */
@media (max-width: 992px) {
  .custom-sidebar:not(.collapsed) {
    width: 200px;
  }
}

@media (max-width: 768px) {
  .custom-sidebar {
    transform: translateX(-100%);
  }

  .custom-sidebar.collapsed {
    transform: translateX(0);
    width: 64px;
  }
}
</style>
